# 重写hook函数，pytest_collection_modifyitems 在收集完测试用例后调用，items代表收集的测试用例对象列表
import pytest
import yaml
from FunctionDemo import Caculator

def pytest_collection_modifyitems(session, config, items):
    for item in items:
        # 测试用例名称和路径改写编码方式，使中文正常显示
        item.name = item.name.encode('utf-8').decode('unicode-escape')
        item._nodeid = item.nodeid.encode('utf-8').decode('unicode-escape')
        
    # 更改执行顺序，倒序执行
    # items.reverse()


def pytest_addoption(parser, pluginmanager):
    pass

# 实例化计算器类，在需要的地方调用
# yield前面的相当setup, yield后面相对teardown
@pytest.fixture(scope='class')
def get_cal():
    print('开始计算')
    cal = Caculator.Caculator()
    yield cal
    print('结束计算')


@pytest.fixture()
def get_data(a):
    # 读取yaml文件测试数据
    with open('./testdata_cal.yaml', encoding='utf-8') as f:
        cal_data = yaml.safe_load(f)[a]
        datas = cal_data['datas']
        dataids = cal_data['dataids']
        
    yield datas, dataids
    print("数据返回成功")
    
@pytest.fixture()
def test_demo1():
    print('验证执行时机')
